package com.aibaixun.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.system.domain.DicmAddress;
import com.aibaixun.system.domain.DicmNvr;
import com.aibaixun.system.domain.dto.DicmNvrPageDto;
import com.aibaixun.system.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.Socket;
import java.util.List;

/**
 * 告警信息管理
 *
 * @author zhangyh
 * @date 2023-09-01
 **/
@RestController
@RequestMapping("/alarm" )
public class AlarmController {

    @Autowired
    private LkUMsgService lkUMsgService;
    @Autowired
    private LkAlarmSourceService lkAlarmSourceService;
    @Autowired
    private LkActivealarmService lkActivealarmService;

//    /**
//     * 获取地址树
//     * @return
//     */
//    @SaCheckPermission
//    @ApiOperation(value = "获取地址树")
//    @PostMapping("/address/tree")
//    public R<List<Tree<String>>> getTree(@RequestBody DicmAddress dto){
//        return dicmAddressService.getTree(dto);
//    }
//
//    /**
//     * 分页查询摄像头列表
//     * @return
//     */
//    @ApiOperation(value = "分页查询摄像头列表")
//    @PostMapping("/nvr/page")
//    public R<Page<DicmNvr>> getPage(@RequestBody DicmNvrPageDto pageDto){
//        Page page = new Page();
//        page.setCurrent(pageDto.getCurrent());
//        page.setSize(pageDto.getSize());
//        page.setSearchCount(true);
//        return dicmNvrService.getPage(page, pageDto);
//    }

    public static void main(String[] args) {
        String host = "1.13.245.175";
        String user = "root";
        String password = "KYzjcx@123456";

        String command = "ps -ef|grep java" +args; // qi

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            channel.setInputStream(null);
            channel.setErrStream(System.err);

            InputStream in = channel.getInputStream();
            channel.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);

            }


            channel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        String host = "172.16.4.171";
//        String user = "86155";
//        String password = "9727698237sd";
////        String command = "bitsadmin /transfer \"JobName\" /download /priority normal https://4c87a3-1905179918.antpcdn.com:19001/b/pkg-ant.baidu.com/issue/netdisk/yunguanjia/BaiduNetdisk_7.44.6.1.exe E:\\hahahahhaha.exe";
//        String command = "E:\\\\t\\d.exe -log -RenderOffScreen";
//
//        try {
//            JSch jsch = new JSch();
//            Session session = jsch.getSession(user, host, 22); // 默认SSH端口为22
//            session.setPassword(password);
//            session.setConfig("StrictHostKeyChecking", "no");
//            session.connect();
//
//            ChannelExec channel = (ChannelExec) session.openChannel("exec");
//            channel.setCommand(command);
//            channel.setInputStream(null);
//            channel.setErrStream(System.err);
//            channel.connect();
//
//            InputStream in = channel.getInputStream();
//            byte[] buffer = new byte[1024];
//            int readBytes;
//            while ((readBytes = in.read(buffer)) != -1) {
//                System.out.print(new String(buffer, 0, readBytes));
//            }
//            System.out.println("命令结束");
//            channel.disconnect();
//            session.disconnect();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }



//    public static void main(String[] args) {
//        String host = "172.16.4.171";
//        String user = "86155";
//        String password = "9727698237sd";
//
//        // 例如： "powershell.exe Get-Content C:\\path\\to\\file.txt"
//        String command = "powershell.exe Get-Content https://4c87a3-1905179918.antpcdn.com:19001/b/pkg-ant.baidu.com/issue/netdisk/yunguanjia/BaiduNetdisk_7.44.6.1.exe";
//
//        String localPath = "E:\\/file.exe";
//
//        try {
//            downloadFileFromServer(host, user, password, command, localPath);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static void downloadFileFromServer(String host, String user, String password, String command, String localPath) throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);

        InputStream in = channel.getInputStream();
        OutputStream out = new FileOutputStream(localPath);
        channel.connect();

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        out.close();
        in.close();
        channel.disconnect();
        session.disconnect();
    }

}
